package nip.com.swiping.adapter;

import java.util.ArrayList;

import nip.com.swiping.adapter.ClassHolder.ViewHolder;
import nip.com.swiping.entity.IconEntity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nip.swipetoremoveitemlistview.R;

public class IconAdapter extends BaseAdapter {

	private Context _context;
	private ArrayList<IconEntity> _arraySource;
	private LayoutInflater _inFlater;
	private OnTouchListener _onTouch;

	public IconAdapter(Context context, ArrayList<IconEntity> arraySource, OnTouchListener _onTouch) {
		_context = context;
		_arraySource = arraySource;
		this._onTouch = _onTouch;
		_inFlater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (_arraySource == null && _arraySource.size() <= 0) {
			return 1;
		} else {
			return _arraySource.size();
		}
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		View row = arg1;
		ViewHolder holder;
		if (arg1 == null) {
			row = _inFlater.inflate(R.layout.icon_lv_layout, null);
			
			holder = new ViewHolder();
			holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
			holder.tvIconName = (TextView)row.findViewById(R.id.tvIconName);
			row.setTag(holder);
		}else
		{
			holder = (ViewHolder) row.getTag();
		}
		
		if(_arraySource != null && _arraySource.size()>0){
			holder.tvIconName.setText(_arraySource.get(arg0).getIconName());
			holder.imgIcon.setImageResource(_arraySource.get(arg0).getIconSource());
			holder.tvIconName.setTag(arg0);
		}
		
		if(this._onTouch != null)
		{
			row.setOnTouchListener(this._onTouch);
		}
		
		return row;
	}

}
